package dk.modified.radioonlineapp;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;

public class UserSettingActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener {

	boolean change = false;
	
	@SuppressWarnings("deprecation")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.settings);
		PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this);
	}
	
	// Opens a dialog if changes were made on the preference screen, notifying the user that he/she should restart the APP
	@Override
	public void onBackPressed() {
		if(change){
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setMessage(getString(R.string.settings_applied))
			       .setCancelable(false)
			       .setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
			           public void onClick(DialogInterface dialog, int id) {
			        	   finish();
			           }
			       });
			AlertDialog alert = builder.create();
			alert.show();
			return;
		} else {
			finish();
		}
        
	}


	// Listener to check if a sharedpreference has been changed, and as is it will change boolean to true as soon as you touch a value.
	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
			String key) {
		// TODO Auto-generated method stub
		if(!change){
			change = true;
		}
		
	}   
}
